perm filename 106A05[1,RWF] blob
sn#728184 filedate 1983-10-26 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00002 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002
C00011 ENDMK
C⊗;
Good
What∧ Are the Standard Functions of Pascal?
Every standard operator and function of a programming language, ideally, is
included for the same reason that a tool is included in a good workman's box:
it's so useful that it's worth the effort of carrying it around. I don't agree
with all the choices the designers of Pascal made, but I'll try to show why
they felt a certain set of functions was useful.
Standard Functions
SQR(X) is the square of X. Pascal does not have a built in operation to raise
a number to an arbitrary power, because the best choice of algorithm depends on
so many considerations only the programmer should decide it. SQR(X) is the most
common case, and others like X↑8=SQR(SQR(SQR(X))), can be built up from it. The
argument may be REAL or INTEGER; the result has the same type. The only pitfall
is overflow. If X is an integer over 2↑{17.5}=185363, or a real over
2↑{63.5}=1.30438E19, the result overflows.
SQRT(X) is the positive square root of X, where X may be REAL or INTEGER; the
result is REAL. The pitfall is that the function is lethally undefined if
X<0. In some situations, exact calculation would give a small positive X,
but the rounding errors of previous approximate computation make X negative.
This pitfall may occur in naive calculations of standard deviations, especially
where the mean is much larger than the standard deviation.
ABS(X) is the absolute value of X, where X may be REAL or INTEGER; the result is
the same. It is used frequently in programming to test whether the difference
between two numbers is small, as in
IF ABS(X-Y)<0.000001 THEN ...
The only pitfall: if X is the integer -2↑{35}= -34359738356, the result
overflows; it is not a likely error.
SIN(X), COS(X) are the standard trigonometric functions sine and cosine, where
X (REAL or INTEGER) is measured in radians; the result is REAL. If X is
measured in degrees, use SIN((π/180)*X), where π=3.141592654. Other direct
trigonometric functions are usually computed from the sine and cosine; the
tangent of x is SIN(X)/COS(X). The major pitfall of SIN and COS is that if
X is much larger than 1, _relatively_ small rounding errors in X (e.g.,
10↑{-8}X= 0.01 when X=10↑6) give rise to errors in SIN(X) and COS(X)
that are _relatively_ large (e.g., 0.01 when |SIN(X)|<1.)
ARCTAN(X) is the angle between -π/2 and π/2 whose tangent is X; X is REAL or
INTEGER, and the REAL result is measured in radians. For a result in degrees,
use (180/π)*ARCTAN(X). The only serious pitfall arises when computing
ARCTAN(X/Y); if Y becomes zero, overflow occurs, even though the desired angle
is well defined. To avoid this, use (say) π/4-ARCTAN(Y/X) when Y is near 0.
EXP(X) is the exponential function e↑x, where e is 2.718281828; X is REAL or
INTEGER, and the result is REAL. Overflow occurs unless X is in the range
-89.415986<x<88.029692. Another minor pitfall is that if X is in the outer
portion of its range, _relatively_ small rounding errors in X result in
_relatively_ larger errors in EXP(X) , by a factor of nearly 200.
LN(X) is the natural (base e) logarithm of X; X is REAL or INTEGER, with a
REAL result. For base 10 logarithms, use LN(X)/LN(10) or the non-standard
function LOG(X). A pitfall is the lethal error X≤0. Less obvious is that if
X is close to 1, a _relatively_ small error in X can result in _relatively_
enormous errors in LN(X); for example, changing X from 1.0001 to 1.0002 doubles
LN(X).
TRUNC(X) is the integer part of a real number X. TRUNC(2.0)=TRUNC(2.99)=2,
TRUNC(-2.0)=TRUNC(-2.99)=-2. TRUNC is useful in going from ``how much'' to
``how many'', as in determining how many standard sized shelves can be cut
from a board _BUT_ it is seldom right to use it if X is negative. If X is
positive, TRUNC(X) is the largest integer that doesn't exceed X, but if X is
negative TRUNC(X) is larger than X, except when X has an exact integer value.
To classify incomes into $1000 ranges for tabulation, one might think of using
TRUNC(INCOME/1000), but that formula, like INCOME DIV 100, lumps the ranges
(-$1000:$0) and ($0:$1000) into the same value: 0. If your installation has
FLOOR(X) (or ENTIER(X)), a non-standard function for the largest integer not
exceeding X, it is probably more useful. Another pitfall is overflow
if |X|≥2↑{35}= 3.4359738356E10. If |X|≥2↑{27}= 1.34217728E8, rounding error
in X may affect the accuracy of TRUNC(X).
To determine how many times A goes into B, both positive real numbers, use
TRUNC(A/B).
ROUND(X) is the integer closest to X. If X has fractional part 1/2, it is
rounded to a larger absolute value. That is, ROUND(2.5)=ROUND(3.499)=3;
ROUND(-2.5)=ROUND(-3.499)=-3. The major pitfall of ROUND is overflow if
X≥2↑{35}. A minor statistical pitfall, if a large fraction of the values of
X have fractional part 1/2, is that none of those values round to 0, which
may be unfairly underrepresented.
ORD(X), where X belongs to any ordinal type (CHAR and enumerated types
especially), is the (INTEGER) ordinal number by which X is enumerated. The
major pitfall is that ORD is implementation dependent for character arguments.
If X is a digit character, however, ORD(X)-ORD('O') is its numerical value.